const {ccclass, property} = cc._decorator;

@ccclass
export default class TileMapSt extends cc.Component {

    /** 角色 */
    @property({ type: cc.Prefab, tooltip: '角色' })
    prefab: cc.Prefab = null;

    /** Tiled C */
    @property({ type: cc.TiledLayer, tooltip: 'Tiled C' })
    tiledLayer: cc.TiledLayer = null;

    /** user layer */
    @property({ type: cc.TiledLayer, tooltip: 'user layer' })
    userLayer: cc.TiledLayer = null;

    /** obstacleLayer */
    @property({ type: cc.TiledObjectGroup, tooltip: 'obstacleLayer' })
    obstacleLayer: cc.TiledObjectGroup = null;

    player: cc.Node = null;

    keys = {};

    onDestroy() {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }

    onKeyDown(event) {
        switch (event.keyCode) {
            case cc.macro.KEY.a:
                this.keys[cc.macro.KEY.a] = true;
                break;
            case cc.macro.KEY.w:
                this.keys[cc.macro.KEY.w] = true;
                break;
            case cc.macro.KEY.s:
                this.keys[cc.macro.KEY.s] = true;
                break;
            case cc.macro.KEY.d:
                this.keys[cc.macro.KEY.d] = true;
                break;
        }
    }

    onKeyUp(event) {
        switch (event.keyCode) {
            case cc.macro.KEY.a:
                delete this.keys[cc.macro.KEY.a];
                break;
            case cc.macro.KEY.w:
                delete this.keys[cc.macro.KEY.w];
                break;
            case cc.macro.KEY.s:
                delete this.keys[cc.macro.KEY.s];
                break;
            case cc.macro.KEY.d:
                delete this.keys[cc.macro.KEY.d];
                break;
        }
    }

    start() {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);

        this.player = cc.instantiate(this.prefab);
        this.player.x = -250;
        this.player.y = -40;
        this.tiledLayer.addUserNode(this.player);
    }

    update (dt) {
        let x = 0, y = 0;
        if (this.keys[cc.macro.KEY.a]) {
            x = this.player.x - 2;
        }
        if (this.keys[cc.macro.KEY.w]) {
            y = this.player.y + 2;
        }
        if (this.keys[cc.macro.KEY.s]) {
            y = this.player.y - 2;
        }
        if (this.keys[cc.macro.KEY.d]) {
            x = this.player.x + 2;
        }
        if (this.keys[cc.macro.KEY.a] || this.keys[cc.macro.KEY.d] || this.keys[cc.macro.KEY.w] || this.keys[cc.macro.KEY.s]) {
            for (var i = this.obstacleLayer.getObjects().length - 1; i >= 0; i--) {
                let obstacle = this.obstacleLayer.getObjects()[i];
                if (!obstacle.visible) {
                    continue;
                }
                let obstacleRect = cc.rect(obstacle.x - 560, obstacle.y - 320 - obstacle.height, obstacle.width, obstacle.height);
                let playerBoundingBox = this.player.getBoundingBox();
                if (this.keys[cc.macro.KEY.a] || this.keys[cc.macro.KEY.d]) {
                    playerBoundingBox.x = x;
                }
                if (this.keys[cc.macro.KEY.w] || this.keys[cc.macro.KEY.s]) {
                    playerBoundingBox.y = y;
                }
                if (obstacleRect.intersects(playerBoundingBox) || obstacleRect.containsRect(playerBoundingBox)) {
                    cc.log(obstacleRect);
                    return;   
                }
            }
            if (this.keys[cc.macro.KEY.a] || this.keys[cc.macro.KEY.d]) {
                this.player.x = x;
            }
            if (this.keys[cc.macro.KEY.w] || this.keys[cc.macro.KEY.s]) {
                this.player.y = y;
            }
            cc.log("after", this.player.x, this.player.y);
        }
    }
}
